package com.xiaoshuidi.cloud.module.finance.mq.listener;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventData;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventListener;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventTypeEnum;
import com.xiaoshuidi.cloud.framework.systemevent.core.data.LeaseSystemEventMobileModify;
import com.xiaoshuidi.cloud.module.finance.enums.finance.FinanceCustomerTypeEnum;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountService;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountWithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Component
@Slf4j
public class LeaseSystemEventMobileModifyListener implements LeaseSystemEventListener {

    @Resource
    private FinanceAccountService financeAccountService;

    @Resource
    private FinanceAccountWithdrawService financeAccountWithdrawService;


    @Resource
    private TransactionDefinition transactionDefinition;

    @Resource
    private PlatformTransactionManager transactionManager;

    @Override
    public LeaseSystemEventTypeEnum getEventType() {
        return LeaseSystemEventTypeEnum.MOBILE_MODIFY;
    }

    @Override
    public void onEvent(LeaseSystemEventData event) {
        LeaseSystemEventMobileModify data = (LeaseSystemEventMobileModify) event;
        log.info("收到修改手机消息 {}#######################", JsonUtils.toJsonString(data));
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        try {
            if (ObjectUtils.isEmpty(data.getUserId())) {
                log.info("新用户id为空，ignore");
            } else {
                //1.finance_account
//                financeAccountService.updateMemberUser(data.getUserId(), data.getUserName(), data.getMobile(),data
//                .getOldUserId());
                financeAccountService.individualAccountUpdatePhone(data.getOldUserId()
                        .toString(), data.getMobile() );
                //finance_account_withdraw
                financeAccountWithdrawService.updateMemberUser(data.getUserId(), data.getUserName(), data.getMobile()
                        , data.getOldUserId());
                transactionManager.commit(transactionStatus);

            }
        } catch (Exception e) {
            log.error("修改手机消息处理异常", e.getMessage());
            transactionManager.rollback(transactionStatus);
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "修改手机消息处理异常!");
        }
    }
}
